LDFLAGS  += 

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# kernel

ARCHINCLUDE    := arch/um/include   \
                  arch/um/drivers

ARCHSRCDIR     := arch/um/kernel    \
				  arch/um/debug 	\
				  arch/um/mm       	\
				  arch/um/drivers   \
				  arch/um/lib

ARCHINCLUDE	   := $(foreach dir,$(ARCHINCLUDE),$(addprefix -I,$(dir)))
ARCHCFLAGS	   := $(ARCHINCLUDE)
ARCHCFLAGS	   += -fno-builtin -nostdinc

$(call add_files_cc,$(call listf_cc,$(ARCHSRCDIR)),arch_kernel,$(ARCHCFLAGS))

ARCHOBJS	= $(call read_packet,arch_kernel arch_driver)

# create kernel target
kernel = $(call totarget,kernel)

$(kernel): $(ARCHOBJS) $(OBJS) $(USER_BINS)
	$(V)echo + ld $@
	$(V)$(LD) -m elf_i386  $(LDFLAGS) -T arch/um/kernel.ld -o $@ $(ARCHOBJS) $(OBJS) -b binary $(USER_BINS)

$(call create_target,kernel)

# -------------------------------------------------------------------

# bootloader
bootfiles = $(call listf_cc, arch/um/boot)
$(foreach f,$(bootfiles),$(call cc_compile,$(f),$(CC),$(HOSTCFLAGS) -Iarch/um/include))

bootloader = $(call totarget,bootloader)

$(bootloader): $(call toobj,$(bootfiles))
	@echo + ld $@
	$(V)$(CC) -Wl,-T,arch/um/boot/bootloader.ld $^ -o $(bootloader)

$(call create_target,bootloader)

# -------------------------------------------------------------------

# create swap.img
SWAPIMG		:= $(call totarget,swap.img)

$(SWAPIMG):
	$(V)dd if=/dev/zero of=$@ bs=1M count=128

$(call create_target,swap.img)

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

$(call finish_all)

run: $(bootloader) $(kernel) $(SWAPIMG)
	$(V)bin/bootloader --kernel=$(kernel) --swap=$(SWAPIMG) --memsize=0x8000000

RUN_PREFIX	:= _binary_$(OBJDIR)_$(USER_PREFIX)
MAKEOPTS	:= --quiet --no-print-directory

run-%: build-%
	$(V)bin/bootloader --kernel=$(kernel) --swap=$(SWAPIMG)  --memsize=0x8000000

build-%: touch
	$(V)$(MAKE) $(MAKEOPTS) "DEFS+=-DTEST=$* -DTESTSTART=$(RUN_PREFIX)$*_out_start -DTESTSIZE=$(RUN_PREFIX)$*_out_size"

TOUCH_FILES		:= arch/um/kernel/proc.c
touch:
	$(V)$(foreach f,$(TOUCH_FILES),$(TOUCH) $(f))
